<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>

</body>
<script>
  function delay(ms) {
  // your complement
  return new Promise(resolve => setTimeout(resolve,ms))
}
//
function timeoutPromise(promise, ms) {
    var timeout = delay(ms).then(function(){
      throw new Error('超市啦啦啦啦啦'+ms)
    })
    return Promise.race([timeout,promise])
}
//测试用例
// 传入1000，1s之后执行alert
const timeout = ms => new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve();
    }, ms);
});

const ajaxA = () => timeout(2000).then(() => {
    console.log('A');
    return 'A';
});

const ajaxB = () => timeout(1000).then(() => {
    console.log('B');
    return 'B';
});

const ajaxC= () => timeout(2000).then(() => {
    console.log('C');
    return 'C';
});

const mergePromise = ajaxArray => {
    // 在这里实现你的代码
    var data =  []
    var promise = Promise.resolve()
    ajaxArray.forEach(item => {
      promise = promise.then(item)
      data.push(promise)
    });
    return Promise.all(data)
};


//测试用例
mergePromise([ajaxA, ajaxB, ajaxC]).then(data => {
    console.log('done');
    console.log(data); // data 为 [A, B, C]
});

// 要求分别输出
// A
// B
// C
// done
// [A, B, C]
delay(1000).then(() => alert('runs after 3 seconds'));
</script>
</html>